package cn.jeeweb.web.modules.cable.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.query.annotation.PageableDefaults;
import cn.jeeweb.common.query.data.PropertyPreFilterable;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.cable.entity.CableType;
import cn.jeeweb.web.modules.cable.service.ICableTypeService;
import cn.jeeweb.web.utils.PageRequest;
import cn.xjzn.framework_Core.ModuleActionType;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @description:电缆类型
 */

@RestController
@RequestMapping("/cable/cableType")
@ViewPrefix("modules/SysConfig")
@Log(title = "cable_type")
public class CableTypeController extends BaseBeanController<CableType> {

    @Autowired
    private ICableTypeService cableTypeService;


    /**
     * 根据页码和每页记录数，以及查询条件动态加载数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "ajaxList", method = {RequestMethod.GET, RequestMethod.POST})
    @PageableDefaults(sort = "id=desc")
    @Log(logType = ModuleActionType.SELECT)
    public void ajaxList(PropertyPreFilterable propertyPreFilterable, HttpServletRequest request, HttpServletResponse response) throws IOException {
        EntityWrapper<CableType> entityWrapper = new EntityWrapper<>(entityClass);
        String cableType = request.getParameter("cableType");
        if (!StringUtils.isEmpty(cableType)) {
            entityWrapper.like("cableType", cableType);
        }
        propertyPreFilterable.addQueryProperty(new String[]{"id", "cableType", "code", "remarks", "createBy", "createDate", "updateBy", "updateDate", "delFlag"});
        // 预处理
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        // 预处理
        Page pageBean = cableTypeService.selectPage(PageRequest.getPage(), entityWrapper);
        FastJsonUtils.print(pageBean);
//		return Response.ok(null,pageBean);
    }

    @PostMapping("add")
    @Log(logType = ModuleActionType.INSERT)
    public Response add(CableType entity, BindingResult result,
                        HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        EntityWrapper<CableType> entityWrapper = new EntityWrapper<>(CableType.class);
        entityWrapper.eq("cableType", entity.getCableType());
        CableType old = cableTypeService.selectOne(entityWrapper);
        if (old != null) {
            return Response.error("保存失败，电缆类型已存在");
        } else {
            cableTypeService.insert(entity);
            return Response.ok("添加成功");
        }
    }


    @GetMapping(value = "{id}/update")
    public ModelAndView update(@PathVariable("id") String id, Model model, HttpServletRequest request,
                               HttpServletResponse response) {
        CableType cableType = cableTypeService.selectById(id);
        model.addAttribute("data", cableType);
        return displayModelAndView("edit");
    }

    @PostMapping("{id}/update")
    @Log(logType = ModuleActionType.UPDATE)
    public Response update(CableType entity, BindingResult result,
                           HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        EntityWrapper<CableType> entityWrapper = new EntityWrapper<>(CableType.class);
        entityWrapper.eq("cableType", entity.getCableType());
        CableType old = cableTypeService.selectOne(entityWrapper);
        if (old != null && !old.getId().equals(entity.getId())) {
            return Response.error("保存失败，电缆类型已存在");
        } else {
            cableTypeService.insertOrUpdate(entity);
            return Response.ok("更新成功");
        }
    }

    @PostMapping("{id}/delete")
    @Log(logType = ModuleActionType.DELETE)
    public Response delete(@PathVariable("id") String id) {
        cableTypeService.deleteById(id);
        return Response.ok("删除成功");
    }

    @PostMapping("batch/delete")
    @Log(logType = ModuleActionType.DELETE)
    public Response batchDelete(@RequestParam("ids") String[] ids) {
        List<String> idList = java.util.Arrays.asList(ids);
        cableTypeService.deleteBatchIds(idList);
        return Response.ok("删除成功");
    }

    @RequestMapping(value = "getCableTypeList", method = {RequestMethod.GET, RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public void getCableTypeList() {
        EntityWrapper<CableType> entityWrapper = new EntityWrapper<>(CableType.class);
        List<CableType> sysConfigList = cableTypeService.selectList(entityWrapper);
        FastJsonUtils.print(sysConfigList);
    }

}